package com.appwoo.txtws.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import com.appwoo.txtws.adapter.BookstoreAdapter;
import com.appwoo.txtws.bean.BookBean;
import com.appwoo.txtws.bean.BookGradeBean;
import com.appwoo.txtws.bean.BookSubjectBean;
import com.appwoo.txtws.component.DBHelper;
import com.appwoo.txtws.component.Global;
import com.appwoo.txtws.component.Logger;
import com.appwoo.txtws.service.BookService;
import com.appwoo.txtws.thread.BookStoreDownloadTask;
import com.appwoo.txtws.wheel.widget.PageListView;
import com.appwoo.txtws.wheel.widget.PageListView.OnRefreshListener;
import java.util.ArrayList;
import java.util.Iterator;

public class BookstoreActivity extends BookActivity
  implements AbsListView.OnScrollListener, View.OnClickListener
{
  private static boolean downloadTaskIsRun;
  public static ArrayList<BookStoreDownloadTask> downloadTasks = new ArrayList();
  private int animTime = 500;
  private boolean blDataTask = false;
  private int bookType;
  private ArrayList<BookBean> books = new ArrayList();
  private DisplayMetrics dm;
  private int firstExtra;
  private int grade;
  private LayoutInflater inflater;
  private Boolean isFoot = Boolean.valueOf(false);
  private String keyword;
  private int listItemHeight;
  private BookstoreAdapter mAdapter;
  private DBHelper mDBHelper;
  private RelativeLayout mLayoutBookStoreHeader;
  private LinearLayout mLayoutBookstoreSearchRequirement;
  private PageListView mLvSearchResultList;
  private ScrollView mSvBookstoreSearchRequirement;
  private TextView mTvBookstoreHeaderSearchRequirement;
  private TextView mTvBookstoreSearchKeyword;
  private View.OnClickListener onSearchRequirementListener = new View.OnClickListener()
  {
    public void onClick(View paramAnonymousView)
    {
      BookstoreActivity.this.searchRequirement = ((BookstoreActivity.SearchRequirementBean)paramAnonymousView.getTag());
      BookstoreActivity.this.isFoot = Boolean.valueOf(false);
      if (BookstoreActivity.this.bookType == 0)
      {
        String str2 = BookstoreActivity.this.searchRequirement.tags;
        if (!str2.equals(BookstoreActivity.this.strGrade))
        {
          BookstoreActivity.this.strGrade = str2;
          BookstoreActivity.this.mTvBookstoreHeaderSearchRequirement.setText(BookstoreActivity.this.searchRequirement.show + "  ");
          BookstoreActivity.this.page = 1;
          BookstoreActivity.this.mAdapter.clearListData();
          BookstoreActivity.this.mAdapter.setGrade(BookstoreActivity.this.strGrade);
          BookstoreActivity.this.loadData();
        }
      }
      String str1;
      do
      {
        do
          return;
        while (BookstoreActivity.this.bookType != 1);
        str1 = BookstoreActivity.this.searchRequirement.tags;
      }
      while (str1.equals(BookstoreActivity.this.subject));
      BookstoreActivity.this.subject = str1;
      BookstoreActivity.this.mTvBookstoreHeaderSearchRequirement.setText(BookstoreActivity.this.searchRequirement.show + "  ");
      BookstoreActivity.this.page = 1;
      BookstoreActivity.this.mAdapter.clearListData();
      BookstoreActivity.this.mAdapter.setSubject(BookstoreActivity.this.subject);
      BookstoreActivity.this.loadData();
    }
  };
  private int page = 1;
  private int pageSize = 10;
  private SearchRequirementBean searchRequirement;
  private boolean searchRequirementMenuOut;
  private BookService service;
  private String strGrade;
  private String subject;

  public static void cancleAllAsyncTask()
  {
    Iterator localIterator;
    if (downloadTasks != null)
      localIterator = downloadTasks.iterator();
    while (true)
    {
      if (!localIterator.hasNext())
      {
        downloadTasks.clear();
        return;
      }
      BookStoreDownloadTask localBookStoreDownloadTask = (BookStoreDownloadTask)localIterator.next();
      if (localBookStoreDownloadTask.isRun())
        setDownloadTaskIsRun(false);
      localBookStoreDownloadTask.stopDownload();
      localBookStoreDownloadTask.recoverProgressBar();
    }
  }

  public static void cancleAsyncTaskByBookname(String paramString)
  {
    Object localObject = null;
    Iterator localIterator = downloadTasks.iterator();
    while (true)
    {
      if (!localIterator.hasNext())
      {
        if (localObject != null)
        {
          if (localObject.isRun())
            setDownloadTaskIsRun(false);
          localObject.stopDownload();
          localObject.recoverProgressBar();
          downloadTasks.remove(localObject);
        }
        return;
      }
      BookStoreDownloadTask localBookStoreDownloadTask = (BookStoreDownloadTask)localIterator.next();
      if (localBookStoreDownloadTask.getBook().getName().equals(paramString))
        localObject = localBookStoreDownloadTask;
    }
  }

  private void drawSearchRequirement(ArrayList<SearchRequirementBean> paramArrayList)
  {
    this.mLayoutBookstoreSearchRequirement.removeAllViews();
    this.searchRequirement = ((SearchRequirementBean)paramArrayList.get(0));
    Iterator localIterator = paramArrayList.iterator();
    while (true)
    {
      if (!localIterator.hasNext())
        return;
      SearchRequirementBean localSearchRequirementBean = (SearchRequirementBean)localIterator.next();
      View localView = this.inflater.inflate(2130903057, null);
      ((TextView)localView.findViewById(2131492967)).setText(localSearchRequirementBean.show);
      localView.setTag(localSearchRequirementBean);
      localView.setOnClickListener(this.onSearchRequirementListener);
      this.mLayoutBookstoreSearchRequirement.addView(localView);
    }
  }

  public static void executeNextAsyncTask()
  {
    if ((downloadTasks != null) && (!downloadTasks.isEmpty()) && (!isDownloadTaskIsRun()) && (!((BookStoreDownloadTask)downloadTasks.get(0)).isRun()))
      ((BookStoreDownloadTask)downloadTasks.get(0)).execute(new String[0]);
  }

  private void findView()
  {
    Intent localIntent = getIntent();
    this.bookType = localIntent.getIntExtra("bookType", -1);
    this.grade = localIntent.getIntExtra("grade", -1);
    this.subject = localIntent.getStringExtra("subject");
    if (this.bookType == -1)
      finish();
    if (this.grade == -1)
      finish();
    this.mTvBookstoreHeaderSearchRequirement = ((TextView)findViewById(2131492981));
    this.mTvBookstoreSearchKeyword = ((TextView)findViewById(2131492983));
    this.mLayoutBookstoreSearchRequirement = ((LinearLayout)findViewById(2131492987));
    this.mLayoutBookStoreHeader = ((RelativeLayout)findViewById(2131492980));
    this.mLvSearchResultList = ((PageListView)findViewById(2131492985));
    this.mSvBookstoreSearchRequirement = ((ScrollView)findViewById(2131492986));
    this.mDBHelper = new DBHelper(this);
    this.service = new BookService(this);
    this.dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(this.dm);
    this.inflater = ((LayoutInflater)getSystemService("layout_inflater"));
  }

  private ArrayList<BookBean> getBookBykeyword()
  {
    String str = this.strGrade;
    if (this.strGrade != null)
    {
      if (this.strGrade.indexOf("18") <= -1)
        break label79;
      str = str + ",10,11,12";
    }
    while (true)
    {
      return this.service.getBookBeanList(Global.USERNAME, this.keyword, this.bookType, str, this.subject, this.page, this.pageSize);
      label79: if (this.strGrade.indexOf("19") > -1)
        str = str + ",13,14,15,16,17";
    }
  }

  public static BookStoreDownloadTask getDownloadTaskByBookname(String paramString)
  {
    Object localObject = null;
    Iterator localIterator = downloadTasks.iterator();
    while (true)
    {
      if (!localIterator.hasNext())
        return localObject;
      BookStoreDownloadTask localBookStoreDownloadTask = (BookStoreDownloadTask)localIterator.next();
      if (localBookStoreDownloadTask.getBook().getName().equals(paramString))
        localObject = localBookStoreDownloadTask;
    }
  }

  private void getListItemHeight()
  {
    this.dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(this.dm);
    int i = this.mLayoutBookStoreHeader.getHeight();
    int j = SyncTutorialActivity.getStatusHeight(this);
    int k = this.dm.heightPixels - i - j;
    this.listItemHeight = (k / 5);
    this.firstExtra = (k % 5);
  }

  private void hiddenSearchRequirementMenu()
  {
    if (this.searchRequirementMenuOut)
    {
      this.searchRequirementMenuOut = false;
      this.mTvBookstoreHeaderSearchRequirement.setCompoundDrawablesWithIntrinsicBounds(0, 0, 2130837990, 0);
      TranslateAnimation localTranslateAnimation = new TranslateAnimation(0.0F, 0.0F, 0.0F, -this.mSvBookstoreSearchRequirement.getMeasuredHeight());
      localTranslateAnimation.setDuration(this.animTime);
      localTranslateAnimation.setAnimationListener(new Animation.AnimationListener()
      {
        public void onAnimationEnd(Animation paramAnonymousAnimation)
        {
          BookstoreActivity.this.mSvBookstoreSearchRequirement.setVisibility(8);
          BookstoreActivity.this.mTvBookstoreHeaderSearchRequirement.setOnClickListener(BookstoreActivity.this);
        }

        public void onAnimationRepeat(Animation paramAnonymousAnimation)
        {
        }

        public void onAnimationStart(Animation paramAnonymousAnimation)
        {
          BookstoreActivity.this.mTvBookstoreHeaderSearchRequirement.setOnClickListener(null);
        }
      });
      this.mSvBookstoreSearchRequirement.startAnimation(localTranslateAnimation);
    }
  }

  private void initSearchRequirement()
  {
    if (this.bookType == 0)
    {
      localArrayList3 = this.mDBHelper.getBookGradeBeanList(this.grade);
      localArrayList4 = new ArrayList();
      localSearchRequirementBean3 = new SearchRequirementBean();
      str = "";
      localIterator2 = localArrayList3.iterator();
      if (!localIterator2.hasNext())
      {
        if (str.endsWith(","))
          str = str.substring(0, -1 + str.length());
        localSearchRequirementBean3.show = "全部";
        localSearchRequirementBean3.tags = str;
        localArrayList4.add(localSearchRequirementBean3);
        this.strGrade = str;
        localIterator3 = localArrayList3.iterator();
        if (localIterator3.hasNext())
          break label199;
        drawSearchRequirement(localArrayList4);
      }
    }
    label199: 
    while (this.bookType != 1)
      while (true)
      {
        ArrayList localArrayList3;
        ArrayList localArrayList4;
        SearchRequirementBean localSearchRequirementBean3;
        Iterator localIterator2;
        Iterator localIterator3;
        return;
        BookGradeBean localBookGradeBean1 = (BookGradeBean)localIterator2.next();
        String str = new StringBuilder(String.valueOf(str)).append(localBookGradeBean1.getGradeId()).toString() + ",";
        continue;
        BookGradeBean localBookGradeBean2 = (BookGradeBean)localIterator3.next();
        SearchRequirementBean localSearchRequirementBean4 = new SearchRequirementBean();
        localSearchRequirementBean4.show = localBookGradeBean2.getGradeName();
        localSearchRequirementBean4.tags = String.valueOf(localBookGradeBean2.getGradeId());
        localArrayList4.add(localSearchRequirementBean4);
      }
    this.strGrade = String.valueOf(this.grade);
    new ArrayList();
    ArrayList localArrayList1 = this.mDBHelper.getBookSubjectBeanList(-1);
    ArrayList localArrayList2 = new ArrayList();
    SearchRequirementBean localSearchRequirementBean1 = new SearchRequirementBean();
    localSearchRequirementBean1.show = "全部";
    localSearchRequirementBean1.tags = "";
    localArrayList2.add(localSearchRequirementBean1);
    this.subject = localSearchRequirementBean1.tags;
    this.strGrade = String.valueOf(this.grade);
    Iterator localIterator1 = localArrayList1.iterator();
    while (true)
    {
      if (!localIterator1.hasNext())
      {
        drawSearchRequirement(localArrayList2);
        return;
      }
      BookSubjectBean localBookSubjectBean = (BookSubjectBean)localIterator1.next();
      SearchRequirementBean localSearchRequirementBean2 = new SearchRequirementBean();
      localSearchRequirementBean2.show = localBookSubjectBean.getName();
      localSearchRequirementBean2.tags = localBookSubjectBean.getName();
      localArrayList2.add(localSearchRequirementBean2);
    }
  }

  public static boolean isDownloadTaskIsRun()
  {
    return downloadTaskIsRun;
  }

  private void loadData()
  {
    this.books = new ArrayList();
    if (!isNetworkAvailable(this))
    {
      alertMessage(getString(2131428051));
      for (int i = 0; ; i++)
      {
        if (i >= 5)
        {
          if (this.mAdapter != null)
          {
            this.mAdapter.addData(this.books);
            this.mAdapter.notifyDataSetChanged();
          }
          closeProgress();
          this.blDataTask = true;
          this.mLvSearchResultList.onLoadMoreComplete();
          if (!this.mLvSearchResultList.getIsNoMore())
          {
            this.mLvSearchResultList.hideFotterMoreView();
            this.mLvSearchResultList.setIsNoMore(true);
          }
          return;
        }
        this.books.add(null);
      }
    }
    new SearchBookTask().execute(new String[0]);
  }

  public static void setDownloadTaskIsRun(boolean paramBoolean)
  {
    downloadTaskIsRun = paramBoolean;
  }

  private void setView()
  {
    this.mTvBookstoreHeaderSearchRequirement.setOnClickListener(this);
    this.mTvBookstoreSearchKeyword.setOnClickListener(this);
    this.mLvSearchResultList.setOnScrollListener(this);
    this.mLayoutBookStoreHeader.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
    {
      public void onGlobalLayout()
      {
        if (BookstoreActivity.this.mAdapter == null)
        {
          Logger.e("onGlobalLayout", "========================================================================");
          BookstoreActivity.this.getListItemHeight();
          BookstoreActivity.this.mAdapter = new BookstoreAdapter(BookstoreActivity.this, BookstoreActivity.this.books, BookstoreActivity.this.listItemHeight, BookstoreActivity.this.firstExtra);
          BookstoreActivity.this.mAdapter.setBookType(BookstoreActivity.this.bookType);
          BookstoreActivity.this.mAdapter.setGrade(BookstoreActivity.this.strGrade);
          BookstoreActivity.this.mAdapter.setSubject(BookstoreActivity.this.subject);
          BookstoreActivity.this.mLvSearchResultList.setAdapter(BookstoreActivity.this.mAdapter);
          BookstoreActivity.this.loadData();
        }
      }
    });
    this.mLvSearchResultList.setOnLoadMoreListener(new PageListView.OnRefreshListener()
    {
      public void onRefresh()
      {
        if ((!BookstoreActivity.this.mLvSearchResultList.getIsNoMore()) && (BookstoreActivity.this.blDataTask))
        {
          BookstoreActivity.this.mLvSearchResultList.prepareForRefreshMore();
          BookstoreActivity.this.isFoot = Boolean.valueOf(true);
          BookstoreActivity.this.loadData();
        }
      }
    });
    initSearchRequirement();
  }

  private void showSearchRequirementMenu()
  {
    if (!this.searchRequirementMenuOut)
    {
      this.searchRequirementMenuOut = true;
      this.mTvBookstoreHeaderSearchRequirement.setCompoundDrawablesWithIntrinsicBounds(0, 0, 2130837991, 0);
      TranslateAnimation localTranslateAnimation = new TranslateAnimation(0.0F, 0.0F, -this.mSvBookstoreSearchRequirement.getMeasuredHeight(), 0.0F);
      localTranslateAnimation.setDuration(this.animTime);
      localTranslateAnimation.setAnimationListener(new Animation.AnimationListener()
      {
        public void onAnimationEnd(Animation paramAnonymousAnimation)
        {
          BookstoreActivity.this.mTvBookstoreHeaderSearchRequirement.setOnClickListener(BookstoreActivity.this);
        }

        public void onAnimationRepeat(Animation paramAnonymousAnimation)
        {
        }

        public void onAnimationStart(Animation paramAnonymousAnimation)
        {
          BookstoreActivity.this.mTvBookstoreHeaderSearchRequirement.setOnClickListener(null);
        }
      });
      this.mSvBookstoreSearchRequirement.setVisibility(0);
      this.mSvBookstoreSearchRequirement.startAnimation(localTranslateAnimation);
    }
  }

  private void toBookstoreSearch()
  {
    Intent localIntent = new Intent(this, BookstoreSearchActivity.class);
    localIntent.putExtra("bookType", this.bookType);
    localIntent.putExtra("grade", this.strGrade);
    localIntent.putExtra("subject", this.subject);
    startActivity(localIntent);
  }

  protected void backKeyCallBack()
  {
    super.backKeyCallBack();
    if (this.searchRequirementMenuOut)
    {
      hiddenSearchRequirementMenu();
      return;
    }
    finish();
  }

  public void onClick(View paramView)
  {
    switch (paramView.getId())
    {
    case 2131492982:
    default:
      return;
    case 2131492981:
      if (this.searchRequirementMenuOut)
      {
        hiddenSearchRequirementMenu();
        return;
      }
      showSearchRequirementMenu();
      return;
    case 2131492983:
    }
    toBookstoreSearch();
  }

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(2130903063);
    findView();
    setView();
  }

  protected void onDestroy()
  {
    Iterator localIterator = downloadTasks.iterator();
    while (true)
    {
      if (!localIterator.hasNext())
      {
        super.onDestroy();
        return;
      }
      ((BookStoreDownloadTask)localIterator.next()).recoverProgressBar();
    }
  }

  protected void onResume()
  {
    super.onResume();
    if (this.mAdapter != null)
      this.mAdapter.notifyDataSetChanged();
  }

  public void onScroll(AbsListView paramAbsListView, int paramInt1, int paramInt2, int paramInt3)
  {
  }

  public void onScrollStateChanged(AbsListView paramAbsListView, int paramInt)
  {
    if ((paramInt == 0) && (paramAbsListView.getLastVisiblePosition() == -1 + paramAbsListView.getCount()) && (!this.mLvSearchResultList.getIsNoMore()) && (this.blDataTask))
    {
      this.mLvSearchResultList.prepareForRefreshMore();
      this.isFoot = Boolean.valueOf(true);
      loadData();
    }
  }

  class SearchBookTask extends AsyncTask<String, Integer, String>
  {
    SearchBookTask()
    {
    }

    protected String doInBackground(String[] paramArrayOfString)
    {
      BookstoreActivity.this.books = BookstoreActivity.this.getBookBykeyword();
      return null;
    }

    protected void onPostExecute(String paramString)
    {
      super.onPostExecute(paramString);
      BookstoreActivity.this.mTvBookstoreHeaderSearchRequirement.setClickable(true);
      BookstoreActivity.this.mTvBookstoreHeaderSearchRequirement.setOnClickListener(BookstoreActivity.this);
      if ((BookstoreActivity.this.books == null) || (BookstoreActivity.this.books.isEmpty()))
      {
        if (BookstoreActivity.this.mAdapter.getCount() == 0);
        for (int i = 0; ; i++)
        {
          if (i >= 5)
          {
            BookstoreActivity.this.mAdapter.addData(BookstoreActivity.this.books);
            BookstoreActivity.this.mAdapter.notifyDataSetChanged();
            BookstoreActivity.this.mLvSearchResultList.onLoadMoreComplete();
            BookstoreActivity.this.blDataTask = true;
            BookstoreActivity.this.mLvSearchResultList.hideFotterMoreView();
            BookstoreActivity.this.mLvSearchResultList.setIsNoMore(true);
            if (!BookstoreActivity.this.isFoot.booleanValue())
            {
              BookstoreActivity.this.closeProgress();
              BookstoreActivity.this.isFoot = Boolean.valueOf(false);
            }
            return;
          }
          BookstoreActivity.this.books.add(null);
        }
      }
      if (BookstoreActivity.this.books.size() < 5)
        if (BookstoreActivity.this.mAdapter.getCount() == 0)
        {
          if (BookstoreActivity.this.books.size() < 5)
            break label346;
          BookstoreActivity.this.mLvSearchResultList.hideFotterMoreView();
          BookstoreActivity.this.mLvSearchResultList.setIsNoMore(true);
        }
      while (true)
      {
        BookstoreActivity.this.blDataTask = true;
        BookstoreActivity.this.mAdapter.addData(BookstoreActivity.this.books);
        BookstoreActivity.this.mAdapter.notifyDataSetChanged();
        BookstoreActivity.this.mLvSearchResultList.onLoadMoreComplete();
        if (!BookstoreActivity.this.isFoot.booleanValue())
        {
          BookstoreActivity.this.closeProgress();
          BookstoreActivity.this.isFoot = Boolean.valueOf(false);
        }
        BookstoreActivity localBookstoreActivity = BookstoreActivity.this;
        localBookstoreActivity.page = (1 + localBookstoreActivity.page);
        return;
        label346: BookstoreActivity.this.books.add(null);
        break;
        if (BookstoreActivity.this.books.size() < 10)
        {
          BookstoreActivity.this.mLvSearchResultList.hideFotterMoreView();
          BookstoreActivity.this.mLvSearchResultList.setIsNoMore(true);
        }
        else
        {
          BookstoreActivity.this.mLvSearchResultList.showFooterMoreView();
          BookstoreActivity.this.mLvSearchResultList.setIsNoMore(false);
        }
      }
    }

    protected void onPreExecute()
    {
      super.onPreExecute();
      BookstoreActivity.this.blDataTask = false;
      if (!BookstoreActivity.this.isFoot.booleanValue())
        BookstoreActivity.this.showProgress("");
      BookstoreActivity.this.mTvBookstoreHeaderSearchRequirement.setClickable(false);
      BookstoreActivity.this.hiddenSearchRequirementMenu();
    }
  }

  class SearchRequirementBean
  {
    String show;
    String tags;

    SearchRequirementBean()
    {
    }
  }
}

/* Location:           C:\Documents and Settings\Administrator\classes_dex2jar.jar
 * Qualified Name:     com.appwoo.txtws.activity.BookstoreActivity
 * JD-Core Version:    0.6.2
 */